﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectronicsMarket.Models;
using ElectronicsMarket.EMServices;
using ElectronicsMarket.InternalService;

namespace ElectronicsMarket.Controllers
{
    public class SearchController : Controller
    {
        public int pageSize = 5;
        private ServicesReleaseSoapClient serviceRelease;

        public SearchController()
        {
            serviceRelease = new ServicesReleaseSoapClient();
        }

        public PartialViewResult SearchList()
        {
            SearchModel searchModel = new SearchModel();
            searchModel.CategoryList = serviceRelease.getAllCategory().ToList();
            searchModel.ManufactureList = serviceRelease.getAllManufacture().ToList();
            return PartialView(searchModel);
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Index(string productName = "noname", int category = 0, int manufacture = 0, int page = 1)
        {
            SearchModel searchModel = new SearchModel();
            searchModel.CategoryList = serviceRelease.getAllCategory().ToList();
            searchModel.ManufactureList = serviceRelease.getAllManufacture().ToList();
            searchModel.ManufactureId = manufacture;
            searchModel.CategoryId = category;
            searchModel.ProductName = productName;
            List<Product> basicList = new List<Product>();
            foreach (var item in serviceRelease.getProductList().ToList())
            {
                Product product = new Product();
                product.Prod = item;
                product.Quantity = serviceRelease.getQuantityOfProduct(item.PRODUCT_ID);
                basicList.Add(product);
            }
            
            LogService.WriteLog(1, "number of products default:" + basicList.Count.ToString());
            LogService.WriteLog(1, "productName: " + productName);
            LogService.WriteLog(1, "categoryId: " + category);
            LogService.WriteLog(1, "manufactureId: " + manufacture);
            
            if (productName.CompareTo("noname") != 0)
            {
                basicList.Clear();
                foreach (var item in serviceRelease.getProductByName(productName).ToList())
                {
                    Product product = new Product();
                    product.Prod = item;
                    product.Quantity = serviceRelease.getQuantityOfProduct(item.PRODUCT_ID);
                    basicList.Add(product);
                }
                LogService.WriteLog(1, "number of products after: " + basicList.Count().ToString());
            }
            if (category != 0)
            {
                basicList = basicList.Where(p => p.Prod.PRODUCT_CATEGORY_ID == category).ToList();
            }
            if (manufacture != 0)
            {
                basicList = basicList.Where(p => p.Prod.PRODUCT_MANUFACTURE_ID == manufacture).ToList();
            }

            if (basicList.Count < serviceRelease.getProductList().ToList().Count)
            {
                searchModel.HaveProduct = true;
                searchModel.NumberOfProduct = basicList.Count;
            }

            searchModel.ProductList = basicList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            Page pageInfo = new Page();
            pageInfo.CurrentPage = page;
            pageInfo.TotalItems = basicList.Count;
            pageInfo.ItemsPerPage = pageSize;
            searchModel.PageInfo = pageInfo;
            return View(searchModel);                                   
        }
    }
}
